<?php
/**
 * test_app
 * @package    spf.applications.test_app
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Test application class.
 *
 * @package    spf.applications.test_app
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class SiTrack extends SPF_Application {
   
   /**
    * Message displayed when there are errors with the user's input.
    */
   const MSG_ERRORS = 'Errors on page.';

   /**
    * Message displayed when a record is updated.
    */
   const MSG_SAVED = 'Record saved successfully.';

   /**
    * Message displayed when a record is deleted.
    */
   const MSG_DELETED = 'Record deleted successfully.';

   /**
    * Message displayed when a record cannot be found.
    */
   const MSG_NOT_FOUND = 'Record not found.';

   /**
    * The name of the application.
    * @var     string
    */
   public $name = 'SiTrack';
   
   /**
    * The version number of the application.
    * @var     string
    */
   public $version = '1.0.0';

   /**
    * Constructor.
    *
    * @return  void
    */
   public function __construct() {
      
      parent::__construct();
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      
      parent::__destruct();
      
   } // __destruct
   
   public function notify( $event, $param = NULL ) {
      
      switch( $event ) {
         case 'system.before_display':
            
            SPF::$controller->assign('current_project', SPF::$session->current_project);
            
            switch( SPF::$route['controller'] ) {
               
               case 'tickets':
                  SPF::$controller->assign('page_title', SPF::$session->current_project['description']);
                  break;
                  
               case 'default':
                  if( SPF::$route['action'] == 'admin' )
                     SPF::$controller->assign('page_title', 'Admin');
                  break;
               
               default:
                  SPF::$controller->assign('page_title', 'Admin');
                  break;
               
            } // switch controller name
            
            break;
            
         case 'tickets.assign':
            
            $user = new User();
            $user->fetch( $param['assigned_user'] );
      
            // send email..
            $email = new SPF_Email();
            
            $email->sender = SPF::$registry->get('mail.from', 'sitrack@localhost');
            $email->subject = "SiTrack - {$param['project_desc']} - Ticket {$param['id']}";
            $email->add_recipient($user->email);
            
            $template = SPF_Template::factory('smarty');
            $template->assign('user', $user->to_array());
            $template->assign('ticket', $param);
            $template->assign('ticket_link', SPF::get_base_url('tickets', 'view', $param['id']));
            
            $email->body = $template->fetch('tickets/email_assigned');
               
            $email->send();
         
            break;
      }
      
      //SPF::log_request();
   
   } // notify
   
   public function handler( $e ) {

      $exception_class = get_class($e);
      $exception_name  = substr($exception_class, 4, -10);
      
      switch( $exception_class ) {
         
         case 'SPF_Access_Denied_Exception':
            // if access was denied because we're not signed in then maybe we should make the user sign in
            if( SPF::$session->user === NULL )
               SPF::redirect('users', 'signin');
            break;
         
      }
      
      $template = SPF_Template::factory('smarty');
      
      $template->assign('exception_name', $exception_name);
      $template->assign('exception', SPF::exception_to_array($e));
      
      // if there is a specific view for this exception then use it
      if( $template->exists('exceptions/'. strtolower($exception_name)) )
         $template->display("exceptions/{$exception_name}");
      
      // otherwise use the default exceptions view
      else
         $template->display('exceptions/default');
      
   } // handler
   
} // test_app

?>
